/**
 * @author luzeyu & liangqiaowei
 */
package tiei.ajp.huffman.utils;

import java.io.*;
import java.util.LinkedList;

public class FileWriter {
    OutputStream outputStream;

    public FileWriter(File outfile) throws IOException {
        outputStream = new FileOutputStream(outfile);
    }

    public FileWriter(OutputStream os) throws IOException {
        outputStream = os;
    }

    public void writeInt(long magicNum) throws IOException {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int m = 0;
        while (magicNum > 0) {
            outputStream.write((int) (magicNum % 256));
            magicNum /= 256;
            m++;
        }
        outputStream.write(m);
        buffer.writeTo(outputStream);
    }

    public void writeData(Byte data) throws IOException {
        outputStream.write(data);
    }

    public void writeData(Byte[] data) throws IOException {
        for (byte d : data) {
            outputStream.write(d);
        }
    }


//    public static void main(String[] args) {
//        String filePath = "output.txt";
//        String content = "xxxxxxxxxxxxxxxxx";
//        try (BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(filePath))) {
//            outputStream.write(content.getBytes());  // 将字符串转换为字节数组写入文件
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
}
